﻿using System.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
using PiscoSourDominio;
using System.Collections.Generic;
using Dapper;
using System;

namespace PiscoSourSOAPServices.Persistencia
{

    public class ReservaDAO
    {

        public string InsertarReserva(Reserva reserva)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {

                    sqlConnection.Open();
                    string sqlQuery = "INSERT INTO PSR_RESERVA(intReserva,dtFecReserva,vchUsuario,intCodLocal,blnEstado,intCantidadPersonas,intCliente) VALUES (@intReserva,@dtFecReserva,@vchUsuario,@intCodLocal,@blnEstado,@intCantidadPersonas,@intCliente)";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intReserva = reserva.IntReserva,
                            dtFecReserva = reserva.dtFecReserva,
                            vchUsuario = reserva.vchUsuario,
                            intCodLocal = reserva.intCodLocal,
                            blnEstado = reserva.blnEstado,
                            intCantidadPersonas = reserva.intCantidadPersonas,
                            intCliente = reserva.intCliente
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string ActualizarReserva(Reserva reserva)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {
                    sqlConnection.Open();
                    string sqlQuery = "UPDATE PSR_RESERVA SET dtFecReserva=@dtFecReserva,vchUsuario=@vchUsuario,intCodLocal=@intCodLocal,blnEstado=@blnEstado,intCantidadPersonas=@intCantidadPersonas,intCliente=@intCliente WHERE intReserva=@intReserva";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intReserva = reserva.IntReserva,
                            dtFecReserva = reserva.dtFecReserva,
                            vchUsuario = reserva.vchUsuario,
                            intCodLocal = reserva.intCodLocal,
                            blnEstado = reserva.blnEstado,
                            intCantidadPersonas = reserva.intCantidadPersonas,
                            intCliente = reserva.intCliente
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public string EliminarReserva(Reserva reserva)
        {
            try
            {
                using (var sqlConnection = ConnectionFactory.GetConnection())
                {

                    sqlConnection.Open();
                    string sqlQuery = "DELETE FROM PSR_RESERVA WHERE intReserva=@intReserva";
                    sqlConnection.Execute(sqlQuery,
                        new
                        {
                            intReserva = reserva.IntReserva
                        });

                    sqlConnection.Close();
                    return "";
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public IEnumerable<Reserva> ListarReservas()
        {
            using (var sqlConnection = ConnectionFactory.GetConnection())
            {
                sqlConnection.Open();
                var sqlQuery = string.Format(@"Select * from PSR_RESERVA");
                var result = sqlConnection.Query<Reserva>(sqlQuery);
                sqlConnection.Close();
                return result;
            }
        }
    }
}